#include "mbappwig.h"
#include "ui_mbappwig.h"
#include "globalval.h"

MbappWig::MbappWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MbappWig)
{
    ui->setupUi(this);

    ui->pushButton_2->setStyleSheet( "background-color: rgb(42, 44, 70)  ;"
                                         "color:white;"
                                   "border-style: outset;"
                                   "border-width: 2px;"
                                   "border-radius: 10px;"
                                   "border-color: beige;"
                                   "font: bold 20px;"
                                   "min-width: 6em;"

                                   "}"
                                   "QPushButton:pressed {"
                                   "background-color: rgb(70, 70, 162);"
                                   "border-style: inset");

    ui->pushButton_3->setStyleSheet( "background-color: rgb(42, 44, 70)  ;"
                                         "color:white;"
                                   "border-style: outset;"
                                   "border-width: 2px;"
                                   "border-radius: 10px;"
                                   "border-color: beige;"
                                   "font: bold 20px;"
                                   "min-width: 6em;"

                                   "}"
                                   "QPushButton:pressed {"
                                   "background-color: rgb(70, 70, 162);"
                                   "border-style: inset");

    ui->label_4->setPixmap(QPixmap(":/new/prefix1/jclose.png"));
    ui->label_4->setScaledContents(true);
    ui->label_5->setPixmap(QPixmap(":/new/prefix1/doorclose.png"));
    ui->label_5->setScaledContents(true);
    ui->label_9->setPixmap(QPixmap(":/new/prefix1/gr.png"));
    ui->label_9->setScaledContents(true);
    ui->label_10->setPixmap(QPixmap(":/new/prefix1/jq.png"));
    ui->label_10->setScaledContents(true);
    ui->label_11->setPixmap(QPixmap(":/new/prefix1/kt.png"));
     ui->label_11->setScaledContents(true);

    connect(ui->pushButton_2,SIGNAL(clicked()),
            this,SLOT(light()));
    connect(ui->pushButton_3,SIGNAL(clicked()),
            this,SLOT(door()));
}

MbappWig::~MbappWig()
{
    delete ui;
}

void MbappWig::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);
    QPixmap map(":/new/prefix1/00.png");
    // 绘制图片
    // 参数1、2 横轴及纵轴坐标
    // 参数3、4 绘制的宽高
    // 参数5    绘制的内容
    painter.drawPixmap(0,0,width(),height(),map);
}


void MbappWig::modbusUpSlot(QByteArray msg)
{
    qDebug() << "modbus反序列化\n";
    QJsonParseError error;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(msg, &error);

    if (error.error != QJsonParseError::NoError) {
        qDebug() << "Error parsing JSON:" << error.errorString();
        return;
    }

    if (!jsonDoc.isObject()) {
        qDebug() << "JSON document is not an object.";
        return;
    }

    QJsonObject jsonObj = jsonDoc.object();

    if (jsonObj.contains("type") && jsonObj["type"].isDouble()){
        int type = jsonObj["type"].toInt();
        qDebug() << "Type:" << type;}

    // 检查是否包含"data"键，并且值是一个数组
    if (jsonObj.contains("data") && jsonObj["data"].isArray()) {
        QJsonArray dataArray = jsonObj["data"].toArray();
        // 遍历数组中的每个数据点对象


        for(const QJsonValue &dataValue : dataArray) {
            int key;
            if (dataValue.isObject()) {
                QJsonObject dataObj = dataValue.toObject();

                // 解析数据点的各个属性
                if (dataObj.contains("key") && dataObj["key"].isDouble()) {
                    key = dataObj["key"].toDouble();
                    qDebug() << "Key:" << key;
                }
                if (dataObj.contains("val") && dataObj["val"].isString()) {
                   QString val = dataObj["val"].toString();
//                    QString val = QString::number(value, 'f', 2);
                    qDebug() << val;
                    qDebug() << "--------" ;
                    switch(key)
                    {
                    case 101:
                        ui->label_6->setText(val);
                        break;
                    case 102:
                        ui->label_7->setText(val);
                        break;
                    case 103:
                        if(val.toInt() == 1)
                        {
                            ui->pushButton_2->setText("开灯了");
                            ui->label_4->setPixmap(QPixmap(":/new/prefix1/jopen.png"));
                            ui->label_4->setScaledContents(true);
                        }
                        else if(val.toInt() == 0)
                        {
                            ui->pushButton_2->setText("关灯了");
                            ui->label_4->setPixmap(QPixmap(":/new/prefix1/jclose.png"));
                            ui->label_4->setScaledContents(true);
                        }
                        break;
                    case 104:
                        val = QString::number(val.toDouble(), 'f', 2);
                        ui->label_8->setText(val);
                        break;
                    case 105:
                        if(val.toInt() == 1)
                        {
                            ui->pushButton_3->setText("开门了");
                            ui->label_5->setPixmap(QPixmap(":/new/prefix1/dooropen.png"));
                            ui->label_5->setScaledContents(true);
                        }
                        else if(val.toInt() == 0)
                        {
                            ui->pushButton_3->setText("关门了");
                            ui->label_5->setPixmap(QPixmap(":/new/prefix1/doorclose.png"));
                            ui->label_5->setScaledContents(true);
                        }
                        break;
                    default:
                        break;
                    }

                }

            }
        }
    }
}

void MbappWig::light()
{
    if(ui->pushButton_2->text() == "开灯了")
    {
        QJsonObject Change103Obj;
        Change103Obj.insert("type",2);
        QJsonObject dataObj;
        dataObj.insert("key",103);
        dataObj.insert("val","0");
        Change103Obj.insert("data",dataObj);
        QJsonDocument send;
        send.setObject(Change103Obj);
        QByteArray byteArr = send.toJson();
        emit ctldown32(byteArr);
        ui->pushButton_2->setText("关灯了");
        ui->label_4->setPixmap(QPixmap(":/new/prefix1/jclose.png"));
        ui->label_4->setScaledContents(true);

    }

    else if(ui->pushButton_2->text() == "关灯了")
    {
        QJsonObject Change103Obj;
        Change103Obj.insert("type",2);
        QJsonObject dataObj;
        dataObj.insert("key",103);
        dataObj.insert("val","1");
        Change103Obj.insert("data",dataObj);
        QJsonDocument send;
        send.setObject(Change103Obj);
        QByteArray byteArr = send.toJson();
        emit ctldown32(byteArr);
        ui->pushButton_2->setText("开灯了");
        ui->label_4->setPixmap(QPixmap(":/new/prefix1/jopen.png"));
        ui->label_4->setScaledContents(true);
    }
}

void MbappWig::door()
{
    if(ui->pushButton_3->text() == "开门了")
    {
        QJsonObject Change103Obj;
        Change103Obj.insert("type",2);
        QJsonObject dataObj;
        dataObj.insert("key",105);
        dataObj.insert("val","0");
        Change103Obj.insert("data",dataObj);
        QJsonDocument send;
        send.setObject(Change103Obj);
        QByteArray byteArr = send.toJson();
        emit ctldown32(byteArr);
        ui->pushButton_3->setText("关门了");
        ui->label_5->setPixmap(QPixmap(":/new/prefix1/doorclose.png"));
        ui->label_5->setScaledContents(true);

    }

    else if(ui->pushButton_3->text() == "关门了")
    {
        QJsonObject Change103Obj;
        Change103Obj.insert("type",2);
        QJsonObject dataObj;
        dataObj.insert("key",105);
        dataObj.insert("val","1");
        Change103Obj.insert("data",dataObj);
        QJsonDocument send;
        send.setObject(Change103Obj);
        QByteArray byteArr = send.toJson();
        emit ctldown32(byteArr);
        ui->pushButton_3->setText("开门了");
        ui->label_5->setPixmap(QPixmap(":/new/prefix1/dooropen.png"));
        ui->label_5->setScaledContents(true);
    }
}


